package com.isnmp.common.snmp.trap;

import java.util.Iterator;
import java.util.Vector;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.PDU;
import org.snmp4j.PDUv1;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;

import com.isnmp.common.base.MyException;
import com.isnmp.common.base.MyLog;

public class TrapReceiverImpl implements CommandResponder {
    public void processPdu(CommandResponderEvent paramCommandResponderEvent) {
        try {
            System.out.println("Reciever SNMP TRAP PDU：" + paramCommandResponderEvent.getPDU());
            UdpAddress localUdpAddress = (UdpAddress) paramCommandResponderEvent.getPeerAddress();
            String str1 = localUdpAddress.getInetAddress().getHostAddress();
            PDU localPDU = paramCommandResponderEvent.getPDU();
            System.out.println("The TRAP Message:" + localPDU);
            TrapMessageInfo localTrapMessageInfo = new TrapMessageInfo();
            localTrapMessageInfo.setAgentSendIP(str1);
            setOIDList(localTrapMessageInfo, localPDU);
            setValueList(localTrapMessageInfo, localPDU);
            localTrapMessageInfo.setOriginalPDU(localPDU);
            Object localObject1;
            Object localObject2;
            if ((localPDU instanceof PDUv1)) {
                localObject1 = (PDUv1) localPDU;
                localTrapMessageInfo.setTrapVersion(1);
                localObject2 = ((PDUv1) localObject1).getEnterprise().toString();
                localTrapMessageInfo.setTrapOID((String) localObject2);
                String str2 = ((PDUv1) localObject1).getAgentAddress().toString();
                localTrapMessageInfo.setPduAgentIP(str2);
                localTrapMessageInfo.setTrapV1GenericType(((PDUv1) localObject1).getGenericTrap());
                if (((PDUv1) localObject1).getGenericTrap() == 6)
                    localTrapMessageInfo.setTrapV1SpecificType(((PDUv1) localObject1).getSpecificTrap());
                localTrapMessageInfo.setTrapName(localTrapMessageInfo.getTrapOID());
                switch (((PDUv1) localObject1).getGenericTrap()) {
                    case 0:
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.0"));
                        break;
                    case 1:
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.1"));
                        break;
                    case 2:
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.2"));
                        break;
                    case 3:
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.3"));
                        break;
                    case 4:
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.4"));
                        break;
                    case 5:
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.5"));
                    default:
                        break;
                }
            } else {
                localTrapMessageInfo.setTrapVersion(2);
                localObject1 = localTrapMessageInfo.getOIDValue(SnmpConstants.snmpTrapAddress.toString());
                if (localObject1 != null)
                    localTrapMessageInfo.setPduAgentIP(((Variable) localObject1).toString());
                localObject2 = localTrapMessageInfo.getOIDValue(SnmpConstants.snmpTrapOID.toString());
                if (localObject2 != null)
                    localTrapMessageInfo.setTrapOID(((Variable) localObject2).toString());
                localTrapMessageInfo.setTrapName(localTrapMessageInfo.getTrapOID());
                if (localObject2 != null)
                    if (((Variable) localObject2).equals(SnmpConstants.coldStart))
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.6"));
                    else if (((Variable) localObject2).equals(SnmpConstants.warmStart))
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.7"));
                    else if (((Variable) localObject2).equals(SnmpConstants.linkDown))
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.8"));
                    else if (((Variable) localObject2).equals(SnmpConstants.linkUp))
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.9"));
                    else if (((Variable) localObject2).equals(SnmpConstants.authenticationFailure))
                        localTrapMessageInfo.setTrapName(Messages.getString("TrapReceiverImpl.10"));
            }
            sendTrap(localTrapMessageInfo);
        } catch (Exception localException) {
            MyLog.err("Process TRAP PDU Erro:" + localException.getMessage());
        }
    }

    void sendTrap(TrapMessageInfo paramTrapMessageInfo) throws MyException {
        synchronized (TrapReceiverServer.eventQueue) {
            TrapReceiverServer.eventQueue.addLast(paramTrapMessageInfo);
            TrapReceiverServer.eventQueue.notifyAll();
        }
    }

    void setOIDList(TrapMessageInfo paramTrapMessageInfo, PDU paramPDU) {
        Vector localVector = paramPDU.getVariableBindings();
        Iterator localIterator = localVector.iterator();
        while (localIterator.hasNext()) {
            VariableBinding localVariableBinding = (VariableBinding) localIterator.next();
            String str = localVariableBinding.getOid().toString();
            paramTrapMessageInfo.getTrapPDUOIDs().add(str);
        }
    }

    void setValueList(TrapMessageInfo paramTrapMessageInfo, PDU paramPDU) {
        Vector localVector = paramPDU.getVariableBindings();
        Iterator localIterator = localVector.iterator();
        while (localIterator.hasNext()) {
            VariableBinding localVariableBinding = (VariableBinding) localIterator.next();
            Variable localVariable = localVariableBinding.getVariable();
            paramTrapMessageInfo.getTrapPDUOIDValues().add(localVariable);
        }
    }
}